﻿using System;
using System.Web.UI.WebControls;
using Com.FastSearch.SharePoint.BL.SearchManager;
using Com.FastSearch.SharePoint.Interfaces;
using Com.FastSearch.Utils;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

namespace Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.Admin
{
    /// <summary>
    /// Backing class for the ESPConnectionConfig.aspx configuration page.
    /// For configuration of connection to ESP qrservers on the web application
    /// levels.
    /// </summary>
    public class ESPConnectionConfig : LayoutsPageBase
    {        
        #region Placeholders

        /// <summary>
        /// The title of the page (in the title bar of the browser).
        /// </summary>
        protected string PageTitle;

        /// <summary>
        /// The title of the page (in SharePoint's title area).
        /// </summary>
        protected string PageTitleInArea;

        /// <summary>
        /// A description of the page (for use under SharePoint's title area).
        /// </summary>
        protected string PageDescription;

        #endregion

        #region Controls

        /// <summary>
        /// Label for exposing error messages.
        /// </summary>
        protected Label lblMessage;

        /// <summary>
        /// Label for exposing information.
        /// </summary>
        protected Label lblInfo;

        /// <summary>
        /// Text-box for entering the qrserver configuration, on the format qrserverhost1:port,qrserverhost2:port,...qrserverhostN:port.
        /// </summary>
        protected InputFormTextBox qrserverValue;

        /// <summary>
        /// OK-button, for saving the qrserver configuration.
        /// </summary>
        protected Button btnOK;

        #endregion

        private SPWebApplication parentWebApp;

        /// <inheritdoc/>
        protected override bool RequireSiteAdministrator
        {
            get { return false; }
        }

        /// <inheritdoc/>
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                //Page Settings
                PageTitle = "Configure ESP QRServer connections";
                PageTitleInArea = "ESP QRServer connections";
                PageDescription = "Use this page to manage the ESP QRServer connections for the current Web Application";

                parentWebApp = SPContext.Current.Site.WebApplication;

                //Allow Unsafe Updates
                SPContext.Current.Web.AllowUnsafeUpdates = true;
            }
            catch (Exception x)
            {
                lblMessage.Text = x.Message;
            }
        }

        /// <inheritdoc/>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            qrserverValue.Text = GetCurrentQRServersValue();
        } 

        private string GetCurrentQRServersValue()
        {            
            return ESPSearchManager.GetSPWebApplicationQRServerconfig(parentWebApp);                             
        }

        /// <summary>
        /// Event handler for the <see cref="btnOK"/> button.
        /// </summary>
        /// <param name="sender">the originator of the event</param>
        /// <param name="e">the event</param>
        public void btnOK_OnClick(object sender, EventArgs e)
        {
            bool noErrors = true;

            try
            {
                string qrserverstring = qrserverValue.Text.Trim();
                //Validate
                if (qrserverstring.Length == 0)
                    throw new Exception("You must provide a QRServer.");

                IESPSearchManager searchManager = null;
                ESPSearchManager.SetSPWebApplicationQRServerconfig(qrserverstring, parentWebApp);                
            }
            catch (Exception x)
            {
                lblMessage.Text = x.Message + x.StackTrace;
                noErrors = false;
            }

            if (noErrors)
            {
                // Todo. check connection.......
                lblInfo.Text += "New QRServers Connection(s) " + qrserverValue.Text + " Saved!";
            }
        }
    }
}